<?php
declare (strict_types = 1);

namespace app\controller\api\v1\expert\order;

use app\controller\api\ApiController;
use app\exception\LogicException;
use app\model\communal\OrderConsultModel;
use app\service\logic\common\order\OrderConsultService;
use app\validate\common\order\ConsultValidate;
use app\validate\common\order\MultiValidate;
use Carbon\Carbon;
use think\App;
use think\response\Json;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class ConsultController extends ApiController
{
    protected $service;

    public function __construct(App $app, OrderConsultService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, MultiValidate::class . '.index');
        $user = $this->request->middleware('user');
        $search['expert_id'] = $user['id'];

        $with = ['belongUser', 'belongExpert'];
        $fun = function ($item) {
            list($item['status_map']) = $this->service->getStatusMap($item['status'], $item);
            $item['purchase_time_map'] = $item['purchase_time'] / 60; // s换算min
            $item['consult_method_map'] = 1 == $item['consult_method'] ? '语音咨询' : '视频咨询';
            $item['payment_type_map'] = $this->service->getPaymentTypeMap($item['payment_type']);
            unset($item['belongUser'], $item['belongExpert']);
            return $item;
        };
        $result = $this->service->getList($params, $search_fields, $search, $with, $fun);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 显示资源
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read()
    {
        $params = $this->request->param();
        $this->validate($params, MultiValidate::class . '.id');

        $with = ['belongUser', 'belongExpert'];
        $fun = function ($item) {
            list($item['status_map']) = $this->service->getStatusMap($item['status'], $item);
            $item['purchase_time_map'] = $item['purchase_time'] / 60; // s换算min
            $item['consult_method'] = 1 == $item['consult_method'] ? '语音咨询' : '视频咨询';
            $item['payment_type_map'] = $this->service->getPaymentTypeMap($item['payment_type']);
            unset($item['belongUser'], $item['belongExpert']);
            return $item;
        };
        $result = $this->service->read($params, $with, $fun);
        return amis($result);
    }

    /**
     * 筛选 企业券
     *
     * @return Json
     */
    public function coupon()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$couponOptions, 'name', 'id', '全部');
        return amis(['options' => $options]);
    }

    /**
     * 筛选 订单状态
     *
     * @return Json
     */
    public function status()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$statusOptions, 'name', 'id', '全部');
        return amis(['options' => $options]);
    }

    /**
     * 排序选项
     *
     * @return Json
     */
    public function order()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$orderOptions);
        return amis(['options' => $options]);
    }

    /**
     * @return Json
     */
    public function judge()
    {
        $options = $this->service->amisOptions(OrderConsultModel::$judgeOptions, 'name', 'name');
        return amis(['options' => $options]);
    }

    /**
     * 填写报告
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws LogicException
     * @throws ModelNotFoundException
     */
    public function report()
    {
        $params = $this->request->only(['id', 'report_judge', 'report_is_spirit', 'report_is_nerve', 'report_desc']);
        $this->validate($params, ConsultValidate::class . '.report');
        $params['report_at'] = Carbon::now()->timestamp;
        $params['status'] = 6;

        $result = $this->service->writeReport($params);
        return amis($result);
    }

    /**
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function cr()
    {
        $params = $this->request->param();
        $this->validate($params, ConsultValidate::class . '.id');

        $result = $this->service->report($params);
        return amis($result);
    }
}
